package com.inhim.pjhim.activity;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.graphics.Bitmap;
import android.net.http.SslError;
import android.os.Build;
import android.support.constraint.ConstraintLayout;
import android.view.View;
import android.webkit.SslErrorHandler;
import android.webkit.WebResourceRequest;
import android.webkit.WebResourceResponse;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.inhim.pjhim.R;
import com.inhim.pjhim.app.BaseActivity;
import com.inhim.pjhim.entity.ReaderInfo;
import com.inhim.pjhim.http.MyOkHttpClient;
import com.inhim.pjhim.http.Urls;
import com.inhim.pjhim.utils.ImageLoaderUtils;
import com.inhim.pjhim.utils.PrefUtils;
import com.inhim.pjhim.utils.TextViewUils;
import com.inhim.pjhim.utils.WXShareUtils;
import com.inhim.pjhim.view.BToast;
import com.inhim.pjhim.view.CircleImageView;
import com.inhim.pjhim.view.CustomRoundAngleImageView;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.HashMap;

import okhttp3.Request;

public class ArticleActivity extends BaseActivity {
    private TextView textview1, textview2, textview3, textview4, textview, textview6,tv_title;
    private CustomRoundAngleImageView custImageview;
    private ImageView iv_back;
    private TextView iv_share;
    private CheckBox checkbox;
    private ReaderInfo.Reader readerInfo;
    private Gson gson;
    private WebView webView;
    String content;
    final String mimeType = "text/html";
    final String encoding = "UTF-8";
    private String title;
    private String description;
    private TextView tv_author_name,tv_author_pastor,tv_author_from,tv_author_content;
    private CircleImageView iv_author_photo;
    private ConstraintLayout in_author;

    @Override
    public Object offerLayout() {
        return R.layout.activity_article;
    }

    @Override
    public void onBindView() {
        gson = new Gson();
        initView();
        getReaderInfo(getIntent().getIntExtra("ReaderId", 0));
    }

    @Override
    public void destory() {

    }

    private void initView() {
        in_author=findViewById(R.id.in_author);
        tv_title=findViewById(R.id.title);
        checkbox = findViewById(R.id.checkbox);
        checkbox.setOnClickListener(v -> {
            MyOkHttpClient myOkHttpClient = MyOkHttpClient.getInstance();
            myOkHttpClient.asyncJsonPost(Urls.collectionReader(readerInfo.getReaderId()), new HashMap(),
                    new MyOkHttpClient.HttpCallBack() {
                        @Override
                        public void onError(Request request, IOException e) {
                            if (checkbox.isChecked()) {
                                checkbox.setChecked(false);
                            } else {
                                checkbox.setChecked(true);
                            }
                        }

                        @Override
                        public void onSuccess(Request request, String result) {
                            hideLoading();
                            try {
                                JSONObject jsonObject = new JSONObject(result);
                                if (jsonObject.getInt("code") != 0) {
                                    BToast.showText(jsonObject.getString("msg"), false);
                                    //收藏失败时将按钮状态还原
                                    if (checkbox.isChecked()) {
                                        checkbox.setChecked(false);
                                    } else {
                                        checkbox.setChecked(true);
                                    }
                                } else {
                                    if (checkbox.isChecked()) {
                                        BToast.showText("收藏成功", true);
                                    } else {
                                        BToast.showText("已取消收藏", true);
                                    }
                                }
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }

                        }
                    });
        });
        textview1 = findViewById(R.id.textview1);
        textview2 = findViewById(R.id.textview2);
        textview3 = findViewById(R.id.textview3);
        textview4 = findViewById(R.id.textview4);
        textview6 = findViewById(R.id.textview6);
        custImageview = findViewById(R.id.custImageview);
        webView = findViewById(R.id.webView);
        initWebview();
        /*webView.getSettings().setJavaScriptEnabled(true);
        //设置 缓存模式
        webView.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);
        // 开启 DOM storage API 功能
        webView.getSettings().setDomStorageEnabled(true);*/
        iv_share = findViewById(R.id.iv_share);
        iv_share.setOnClickListener(v -> WXShareUtils.show(ArticleActivity.this, Urls.shareH5(readerInfo.getReaderId()), title, description));
        iv_back = findViewById(R.id.iv_back);
        iv_back.setOnClickListener(v -> {
            finish();
        });
        textview = findViewById(R.id.textview);
        tv_author_name = findViewById(R.id.tv_author_name);
        tv_author_pastor = findViewById(R.id.tv_author_pastor);
        tv_author_from = findViewById(R.id.tv_author_from);
        tv_author_content = findViewById(R.id.tv_author_content);
        iv_author_photo = findViewById(R.id.iv_author_photo);
    }

    @SuppressLint("SetJavaScriptEnabled")
    private void initWebview() {
        WebSettings webSettings = webView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        //启用应用缓存
        webSettings.setAppCacheEnabled(false);
        webSettings.setDatabaseEnabled(false);
        //开启DOM缓存，关闭的话H5自身的一些操作是无效的
        webSettings.setDomStorageEnabled(true);
        //适应屏幕
        webSettings.setLoadWithOverviewMode(true);
        webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        webSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);
        webView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
                //解决android7.0以后版本加载异常问题
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                    view.loadUrl(request.getUrl().toString());
                } else {
                    view.loadUrl(request.toString());
                }
                return true;
            }

            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
            }

            @TargetApi(Build.VERSION_CODES.M)
            @Override
            public void onReceivedHttpError(WebView view, WebResourceRequest request, WebResourceResponse errorResponse) {
                super.onReceivedHttpError(view, request, errorResponse);
                // 这个方法在6.0才出现
                int statusCode = errorResponse.getStatusCode();
                if (404 == statusCode || 500 == statusCode) {
                }
            }

            @Override
            public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
                //接受证书
                handler.proceed();
            }
        });
    }

    private void getReaderInfo(int readerId) {
        MyOkHttpClient myOkHttpClient = MyOkHttpClient.getInstance();
        myOkHttpClient.asyncGet(Urls.getReaderInfo(readerId, PrefUtils.getString("token", "")), new MyOkHttpClient.HttpCallBack() {
            @Override
            public void onError(Request request, IOException e) {
                BToast.showText("网络错误", false);
            }

            @Override
            public void onSuccess(Request request, String result) {
                ReaderInfo readerInfos = gson.fromJson(result, ReaderInfo.class);
                if (readerInfos.getCode() == 0) {
                    readerInfo = readerInfos.getReader();
                    Glide.with(ArticleActivity.this).load(readerInfo.getCover()).into(custImageview);
                    title = readerInfo.getTitle();
                    description = readerInfo.getSynopsis();
                    if (description != null && !"".equals(description)) {
                        textview.setVisibility(View.VISIBLE);
                        textview.setText(description);
                    }
                    checkbox.setChecked(readerInfo.getCollectionStatus());
                    textview1.setText(readerInfo.getTitle());
                    if(readerInfo.getReaderTypeText()!=null){
                        tv_title.setText(readerInfo.getReaderTypeText());
                    }
                    if(readerInfo.getTagName()==null || "".equals(readerInfo.getTagName())){
                        textview2.setVisibility(View.GONE);
                    }else{
                        textview2.setVisibility(View.VISIBLE);
                        textview2.setText(readerInfo.getTagName());
                    }
                    //textview6.setText("上传时间："+readerInfo.getCreateTime());
                    if (null != readerInfo.getAuthor() && !"".equals(readerInfo.getAuthor().getName())) {
                        textview3.setVisibility(View.VISIBLE);
                        textview3.setText("作者："+readerInfo.getAuthor().getName());
                    }else {
                        textview3.setVisibility(View.GONE);
                    }
                    textview4.setText(readerInfo.getReadAmount()+"人已阅读");
                    content = readerInfo.getContent();
                    if (content == null || content.length() == 0) {
                        content = "暂无内容";
                    }
                    if(readerInfo!=null && readerInfo.getAuthor()!=null){
                        /*in_author.setVisibility(View.VISIBLE);
                        TextViewUils.setText(tv_author_name,readerInfo.getAuthor().getName());
                        TextViewUils.setText(tv_author_pastor,readerInfo.getAuthor().getIdentity());
                        TextViewUils.setText(tv_author_from,"来自:"+readerInfo.getAuthor().getArea());
                        TextViewUils.setText(tv_author_content,readerInfo.getAuthor().getSynopsis());
                        ImageLoaderUtils.setImage(readerInfo.getAuthor().getHeadPortrait(),iv_author_photo);*/
                    }else{
                        in_author.setVisibility(View.GONE);
                    }
                    webView.loadDataWithBaseURL(null, content, mimeType, encoding, null);
                    webView.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
                } else {
                    BToast.showText(readerInfos.getMsg(), false);
                }
            }
        });
    }

}
